package views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import models.Odontologo;
import models.OdontologosListModel;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import controllers.Pantalla_Principal_Controller;
import info.clearthought.layout.TableLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Image;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Pantalla_Principal extends JFrame {

	private Actions l= new Actions();
	private Pantalla_Turnos fmrTurnos;
	private JMenu menuOdontologos;
	private JMenu menuAgendas;
	private JPanel panelPrincipal;
	private JButton btnCrearAgenda;
	private JLabel lblSeleccionarOdontologo;
	private JComboBox comboSeleccionarOdontologo;
	private JMenuItem itmSalirDelSistema;
	private JMenu menuSalir;
	private JMenuItem itmAsignarCancelarTurno;

	private JMenuItem itmCrearOdontologo;
	
	private JMenuItem itmCrearAgenda;
	private JMenu menuTurnos;
	private JMenuBar menuBar;
	private Crear_Odontologo fmrCrear_Odontologo;
	private Crear_Agenda fmrCrearAgenda;
	private Pantalla_Principal_Controller controller=Pantalla_Principal_Controller.getInstancia();
	private DefaultComboBoxModel comboSeleccionarOdontologoModel;
	
	private static Pantalla_Principal instancia;
	
	public static Pantalla_Principal getInstancia()
	{
		if(instancia==null)
			instancia= new Pantalla_Principal();
		return instancia;
	}
	
	private Odontologo seleccionado;
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Pantalla_Principal inst = Pantalla_Principal.getInstancia();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	private Pantalla_Principal() {
		super();
		initGUI();
		setExtendedState(MAXIMIZED_BOTH);
		panelPrincipal.setVisible(false);
	}
	

	private void initGUI() {
		try {

			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			this.setTitle("Pantalla Principal");
			this.setPreferredSize(new java.awt.Dimension(1272, 778));
			getContentPane().setBackground(new java.awt.Color(214,217,223));
			{
				panelPrincipal = new JPanel();
				TableLayout panelPrincipalLayout = new TableLayout(new double[][] {{TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL}, {TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL}});
				panelPrincipalLayout.setHGap(5);
				panelPrincipalLayout.setVGap(5);
				AnchorLayout panelPrincipalLayout1 = new AnchorLayout();
				getContentPane().add(panelPrincipal, new AnchorConstraint(84, 561, 467, 26, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				panelPrincipal.setPreferredSize(new java.awt.Dimension(676, 265));
				panelPrincipal.setLayout(panelPrincipalLayout1);
				panelPrincipal.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)));
			}
			{
				lblSeleccionarOdontologo = new JLabel();
				getContentPane().add(lblSeleccionarOdontologo, new AnchorConstraint(94, 271, 116, 60, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblSeleccionarOdontologo.setText("Seleccione un odontologo");
				panelPrincipal.add(lblSeleccionarOdontologo, new AnchorConstraint(92, 325, 326, 83, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblSeleccionarOdontologo.setPreferredSize(new java.awt.Dimension(164, 62));
			}
			{
				comboSeleccionarOdontologo = new JComboBox();
				getContentPane().add(comboSeleccionarOdontologo, new AnchorConstraint(147, 267, 186, 60, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				//comboSeleccionarOdontologo.setModel(comboSeleccionarOdontologoModel);
				panelPrincipal.add(comboSeleccionarOdontologo, new AnchorConstraint(326, 782, 454, 83, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				comboSeleccionarOdontologo.setPreferredSize(new java.awt.Dimension(473, 34));
				comboSeleccionarOdontologo.addActionListener(l);
			}
			{
				btnCrearAgenda = new JButton();
				panelPrincipal.add(btnCrearAgenda);
				getContentPane().add(btnCrearAgenda, new AnchorConstraint(141, 503, 198, 367, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCrearAgenda.setText("Crear Agenda");
				btnCrearAgenda.addActionListener(l);
				panelPrincipal.add(btnCrearAgenda, new AnchorConstraint(775, 949, 945, 782, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCrearAgenda.setPreferredSize(new java.awt.Dimension(113, 45));
			}
			{
				menuBar = new JMenuBar();
				setJMenuBar(menuBar);
				menuBar.setBackground(new java.awt.Color(64,128,128));
				menuBar.setForeground(new java.awt.Color(64,128,128));
				{
					menuAgendas = new JMenu();
					menuBar.add(menuAgendas);
					menuAgendas.setText("Agendas");
					{
						itmCrearAgenda = new JMenuItem();
						menuAgendas.add(itmCrearAgenda);
						itmCrearAgenda.setText("Crear Agenda");
						itmCrearAgenda.addActionListener(l);
					}
			
				}
				{
					menuOdontologos = new JMenu();
					menuBar.add(menuOdontologos);
					menuOdontologos.setText("Odontologos");
					{
						itmCrearOdontologo = new JMenuItem();
						menuOdontologos.add(itmCrearOdontologo);
						itmCrearOdontologo.setText("Crear Odontologo");
						itmCrearOdontologo.addActionListener(l);
					}
				}
				{
					menuTurnos = new JMenu();
					menuBar.add(menuTurnos);
					menuTurnos.setText("Turnos");
					{
						itmAsignarCancelarTurno = new JMenuItem();
						menuTurnos.add(itmAsignarCancelarTurno);
						itmAsignarCancelarTurno.setText("Asignar/Cancelar turno");
						itmAsignarCancelarTurno.addActionListener(l);
					}
				}
				{
					menuSalir = new JMenu();
					menuBar.add(menuSalir);
					menuSalir.setText("Salir");
					{
						itmSalirDelSistema = new JMenuItem();
						menuSalir.add(itmSalirDelSistema);
						itmSalirDelSistema.setText("Salir Del Sistema");
						itmSalirDelSistema.addActionListener(l);
					}
				}
			}
			{
				fmrTurnos= new Pantalla_Turnos(this);
				fmrCrear_Odontologo= new Crear_Odontologo(this);
				fmrCrearAgenda= new Crear_Agenda(this);
			}
			pack();
			this.setSize(1272, 778);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public class Actions implements ActionListener{

		@Override
		
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			panelPrincipal.setVisible(false);
			if(source == itmSalirDelSistema)
			{
				System.exit(0);
			}
			else if(source== itmCrearAgenda)
			{
				panelPrincipal.setVisible(true);
				comboSeleccionarOdontologoModel= new DefaultComboBoxModel();
				for(int i=0; i< controller.getOdontologos().size(); i++)
				{
					System.out.println("elemento= "+ controller.getOdontologos().elementAt(i).getApellido()
							+", " + controller.getOdontologos().elementAt(i).getNombre()
							+"; Mat: " 
							+controller.getOdontologos().elementAt(i).getMatricula());
					comboSeleccionarOdontologoModel.insertElementAt(controller.getOdontologos().elementAt(i).getApellido()
							+", " + controller.getOdontologos().elementAt(i).getNombre()
							+".		               Mat:" 
							+controller.getOdontologos().elementAt(i).getMatricula() , i);
				}
				
				comboSeleccionarOdontologo.setModel(comboSeleccionarOdontologoModel);
				if(comboSeleccionarOdontologo.getItemCount()!=0){
					comboSeleccionarOdontologo.setSelectedIndex(0);
					controller.setOdontologoActual((String)comboSeleccionarOdontologo.getSelectedItem());
				}
				else
				{
					panelPrincipal.setVisible(false);
					JOptionPane.showMessageDialog(null, "No hay odontologos creados, cree alguno y vuelva a ingresar esta opcion");
				}
				
			}
			else if (source == itmCrearOdontologo)
			{
				setEnabled(false);
				fmrCrear_Odontologo.setVisible(true);
			}
			else if (source == itmAsignarCancelarTurno)
			{
				setEnabled(false);
				fmrTurnos.setVisible(true);
				fmrTurnos.completarComboOdontologos();
			}
			else if(source == btnCrearAgenda)
			{
				setEnabled(false);
				fmrCrearAgenda.setVisible(true);
				fmrCrearAgenda.setOdontologo();
			}
			else if(source == comboSeleccionarOdontologo)
			{
				panelPrincipal.setVisible(true);
				controller.setOdontologoActual((String)comboSeleccionarOdontologo.getSelectedItem());
			}
		
		}
		
	}

}
